package com.baixun.controller;

import com.baixun.constant.MessageConstant;
import com.baixun.entity.PageResult;
import com.baixun.entity.QueryPageBean;
import com.baixun.entity.Result;
import com.baixun.pojo.Role;
import com.baixun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加用户角色
     *
     * @param role
     */
    @PostMapping("/add")
    public Result addRole(@RequestBody Role role) {

        try {
             roleService.addRole(role);
             return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL,e.getMessage());
        }
    }

    /**
     * 更新用户角色
     *
     * @param role
     */
    @PutMapping("/update")
    public Result updateRoleById(@RequestBody Role role) {

        try {
             roleService.updateRoleById(role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL,e.getMessage());
        }
    }

    /**
     * 改变角色的状态
     *
     * @param id,status
     */
    @PatchMapping("/updateStatus/{id}/{status}")
    public Result updateRoleStatus(@PathVariable("id") Integer id, @PathVariable("status") Byte status) {

        try {
            roleService.updateRoleStatus(id, status);
            return new Result(true, MessageConstant.UPDATE_ROLESTATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ROLETATUS_FAIL,e.getMessage());
        }

    }

    /**
     * 永久删除角色
     *
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {

        try {
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL,e.getMessage());
        }

    }

    /**
     * 通过id查询角色
     *
     * @param id
     */
    @GetMapping("/select/{id}")
    public Result selectById(@PathVariable("id") Integer id) {

        try {
            Role role = roleService.selectById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL,e.getMessage());
        }

    }

    /**
     * 通过条件分页查询角色
     *
     * @param queryPageBean
     */
    @GetMapping("/select")
    public Result selectByConditionPage(@RequestBody QueryPageBean<String> queryPageBean) {

        try {
            PageResult pageResult = roleService.selectByConditionPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL,e.getMessage());
        }

    }
}
